﻿using Auth.Application.IServices.Base;
using Auth.Dtos.Base;
using Auth.Entities.Base;
using Auth.IRepository;
using Auth.Models;
using Auth.Models.Base;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Auth.Application.Services.Base
{
    public class OrganizationService : IOrganizationService
    {
        /// <summary>
        /// 工作单元
        /// </summary>
        private readonly IUnitOfWork UnitOfWork;

        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper _mapper;

        public OrganizationService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            UnitOfWork = unitOfWork;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<Guid?> InsertAsync(OrganizationDto dto)
        {
            Organization entity = _mapper.Map<Organization>(dto);

            // 判断Value是否已存在
            if (await UnitOfWork.Context.HasValueAsync<Organization>(nameof(entity.Name), entity.Name))
            {
                return null;
            }
            else
            {
                await UnitOfWork.Context.InsertAsync(entity);
                return entity.Id;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(OrganizationDto dto)
        {
            Organization entity = _mapper.Map<Organization>(dto);
            bool result = await UnitOfWork.Context.DeleteAsync(entity);

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(OrganizationDto dto)
        {
            Organization entity = _mapper.Map<Organization>(dto);
            bool result = await UnitOfWork.Context.UpdateAsync(entity);

            return result;
        }

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<OrganizationDto> GetAsync(Guid id)
        {
            Organization entity = await UnitOfWork.Context.GetAsync<Organization>(id);
            OrganizationDto dto = _mapper.Map<OrganizationDto>(entity);

            return dto;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task<PagedList<OrganizationDto>> GetPagedListAsync(OrganizationModel model)
        {
            IEnumerable<Organization> entities = await UnitOfWork.Context.GetPagedListAsync<Organization, OrganizationModel>(model.Pagination.Page, model.Pagination.ItemsPerPage, model);
            long count = await UnitOfWork.Context.CountAsync<Organization, OrganizationModel>(model);

            IEnumerable<OrganizationDto> list = _mapper.Map<IEnumerable<OrganizationDto>>(entities);
            return new PagedList<OrganizationDto> { List = list, Count = count };
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<IEnumerable<OrganizationDto>> CountAsync(OrganizationModel model)
        {
            IEnumerable<Organization> entities = await UnitOfWork.Context.GetListAsync<Organization, OrganizationModel>(model);
            IEnumerable<OrganizationDto> list = _mapper.Map<IEnumerable<OrganizationDto>>(entities);

            return list;
        }
    }
}
